<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@include file="/context/mytags.jsp" %>
<div class="portlet box green">
    <div class="portlet-title">
        <div class="caption">
            <i class="fa fa-gift"></i>患者
        </div>
    </div>
    <div class="portlet-body form">
        <form id="patientForm" class="form-horizontal form-bordered" action="/patient/save" method="post">
            <div class="form-group">
                <label class="col-md-3 control-label">真实姓名:</label>
                <div class="col-md-4">
                    <div class="input-icon right">
                        <i class="fa"></i>
                        <input type="text" name="name" class="form-control" required
                               value="${patient.name}" maxlength="100" placeholder="请输入真实姓名"
                               onblur="allTrim(this);"/>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3 control-label">用户名:</label>
                <div class="col-md-4">
                    <div class="input-icon right">
                        <i class="fa"></i>
                        <input type="text" name="userName" class="form-control" required
                               value="${patient.userName}" maxlength="100" placeholder="请输入用户名"
                               onblur="allTrim(this);"/>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3 control-label">头像:</label>
                <div class="col-md-9">
                    <div class="fileinput fileinput-new" data-provides="fileinput">
                        <div class="fileinput-preview thumbnail" data-trigger="fileinput" id="checkAvatarUrl"
                             style="width: 200px; height: 150px;">
                            <c:if test="${not empty patient.avatarUrl}">
                                <img src="<c:url value="/common/showImage?filePath=${patient.avatarUrl}"></c:url>"
                                     alt=""/>
                            </c:if>
                        </div>
                        <div>
                        <span class="btn default btn-file">
                            <span class="fileinput-new">选择图片</span>
                            <span class="fileinput-exists">编辑</span>
                            <input type="file" name="avatarUrl" value="${patient.avatarUrl}">
                        </span>
                            <a href="javascript:;" class="btn red fileinput-exists" data-dismiss="fileinput">删除</a>
                        </div>
                    </div>
                </div>

            </div>
            <div class="form-group">
                <label class="col-md-3 control-label">密码:</label>
                <div class="col-md-4">
                    <div class="input-icon right">
                        <i class="fa"></i>
                        <input type="text" name="password" class="form-control" required
                               value="${patient.password}" maxlength="16" placeholder="6-16个字符"
                               onblur="allTrim(this);"/>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3 control-label">年龄:</label>
                <div class="col-md-4">
                    <input type="number" name="age" class="form-control" value="${patient.age}" maxlength="10"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3 control-label">性别</label>
                <div class="col-md-4">
                    <div class="radio-list">
                        <label class="radio-inline">
                            <input type="radio" name="sex" value="1" <c:if test="${patient.sex == 1}">checked</c:if>> 男
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="sex" value="0" <c:if test="${patient.sex == 0}">checked</c:if>> 女
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="sex" value="2"
                                   <c:if test="${patient.sex != 0 && patient.sex != 1}">checked</c:if>> 保密 </label>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3 control-label">手机号:</label>
                <div class="col-md-4">
                    <div class="input-icon right">
                        <i class="fa"></i>
                        <input type="text" name="phone" class="form-control" required
                               value="${patient.phone}" maxlength="20" onblur="allTrim(this);"/>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3 control-label">邮箱:</label>
                <div class="col-md-4">
                    <input type="email" name="email" class="form-control" value="${patient.email}" maxlength="100"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-3 control-label">地址:</label>
                <div class="col-md-4">
                    <input type="text" name="addressDetail" class="form-control" value="${patient.addressDetail}"
                           maxlength="65535"/>
                </div>
            </div>
            <div class="form-actions">
                <div class="col-md-9 col-md-offset-3">
                    <button type="button" class="btn btn-primary" onclick="savePatient(this)">
                        <spring:message code="btn.save"></spring:message>
                    </button>
                    <button type="reset" class="btn btn-default" onclick="cancel()">
                        <spring:message code="btn.return"></spring:message>
                    </button>
                </div>
            </div>
            <input type="hidden" name="checkAvatarUrl">
            <input type="hidden" name="id" value="${patient.id}">
        </form>
    </div>
</div>
<script type="text/javascript" src="<c:url value="/static/custom/datepicker-util.js"></c:url>"></script>
<script type="text/javascript">
    $(function () {
        $('#patientForm').validate();
    });

    /**
     * 患者信息新建或修改
     * @param obj
     */
    function savePatient(obj) {
        if ($('#patientForm').valid()) {
            var checkAvatarUrl = $("#checkAvatarUrl").children("img").attr("src");
            if (checkAvatarUrl != undefined && $.trim(checkAvatarUrl).length > 0) {
                $("input[name='checkAvatarUrl']").val(checkAvatarUrl);
            }
            var formData = new FormData($('#patientForm')[0]);
            $.ajax({
                type: 'post',
                url: '<c:url value="/patient/save"></c:url>',
                data: formData,
                async: false,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json',
                success: function (data) {
                    alert(messages[data.result]);
                    search();
                    $(".panel-search").show();
                }
            });
        }
    }
</script>